<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.wxtf.platform.plugins.user.*"%>

<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
	UserBean user = (UserBean)request.getSession().getAttribute("userBean");
	String _db = user.getDbType();
	String org = user.getCurOrgName();
	String org_id = user.getCurOrgId();
	String userName = user.getUserrelName();
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<base href="<%=basePath%>"/>
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta http-equiv="pragma" content="no-cache"/>
		<meta http-equiv="cache-control" content="no-cache"/>
		<meta http-equiv="expires" content="0"/>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>年度计划申请</title>
		
		<link type="text/css" rel="stylesheet" href="ligerUI/skins/Aqua/css/ligerui-all.css" />
		<link type="text/css" rel="stylesheet" href="ligerUI/skins/<%=user.getUserCss()%>/css/ligerui-all.css" />
		<link type="text/css" rel="stylesheet" href="ligerUI/skins/ligerui-icons.css" />
		<link type="text/css" rel="stylesheet" href="resource/css/style.css" />

		<script type="text/javascript" src="js/jquery/jquery-1.7.2.js"></script>
		<script type="text/javascript" src="ligerUI/js/core/base.js"></script>
		<script type="text/javascript" src="ligerUI/js/ligerui.min.js"></script>
		<script type="text/javascript" src="js/common.js"></script>
		<script type="text/javascript" src="js/validata.js"></script>

		<script type="text/javascript" src="ligerUI/js/plugins/ligerGrid.js"></script>
		<script type="text/javascript" src="ligerUI/js/plugins/ligerForm.js"></script>
	    <script src="ligerUI/js/plugins/ligerTextBox.js" type="text/javascript"></script>
	    <script src="ligerUI/js/plugins/ligerSpinner.js" type="text/javascript"></script>		
<script type="text/javascript">
var _db = '<%=_db%>';
var org = '<%=org%>';
var org_id = '<%=org_id%>';
var userName = '<%=userName%>';
var shijian = getCurDateTime();
_jsonObj.classname = "com.wxtf.platform.main.contraceptives.TmaIncomePlanMainAction";
_jsonObj.methodname = "queryPage";

var manager;//add
var detail;//detail
var grid;//list
var win = true;
var r_id;
$(function(){
	grid = $("#grid").ligerGrid({
		url: _url, parms: _jsonObj, width: '100%', height:'100%',enabledSort:false,async:false,
		usePager: true, rownumbers: true, alternatingRow: true, checkbox: true,pageSize:100,
		columns: [
			{ display: 'tmaipm_id', name: 'tmaipm_id', hide: true, width:1 , type: 'string'},
			{ display: 'org_id', name: 'org_id', hide: true, width:1 , type: 'string'},
			{ display: 'caption', name: 'caption', hide: true, width:1 , type: 'string'},
			{ display: '计划入库编号', name: 'tmaipm_code', width:200 , type: 'string',render: function(record, index, value, oth){
					if(record.tmaipm_is_valid=="1"){
						return "<a href='javascript:Detail(" + JSON.stringify(record) + ")'>"+record.tmaipm_code+"</a>";
					}else if(record.tmaipm_is_valid=="0"){
						return "<a href='javascript:edit(" + JSON.stringify(record) + ")'>"+record.tmaipm_code+"</a>";
					}					
				}
			},
			{ display: '计划年份', name: 'tmaipm_year', width:100 , type: 'string'},
			{ display: '计划入库日期', name: 'tmaipm_time', width: 120, align: 'left' ,type: 'string'},
			{ display: '编报单位', name: 'caption', width: 100, align: 'left' ,type: 'string'},			
			{ display: '录入人', name: 'create_by', width: 100, align: 'left' ,type: 'string'},
			{ display: '状态', name: 'tmaipm_is_valid', width: 100, align: 'left' ,type: 'string',render: function(record, index, value, oth){
					if(value=="1"){
						return "有效";
					}else if(value=="0"){
						return "无效";
					}
				}
			},
			{ display: '编辑', name: 'bianji', width: 50, align: 'center', type: 'sting',render: function(record, index, value, oth){
					if(record.tmaipm_is_valid=="0"){
						return "<img src='ligerUI/skins/icons/edit.gif' onclick='edit(" + JSON.stringify(record) + ")'></img>";
					}
				}
			}
		],
		toolbar: {
			items: [
				{ text: '新建', click: function(){if(win){addData();}}, icon: 'add' },
				{ line: true },
				{ text: '删除', click: function(){if(win){delData();}}, icon: 'delete'}
			]
		}
	});
	//add
	manager = $("#manager").ligerGrid({
		url: _url, width: '890', height:'325',async:false,delayLoad:true,
		usePager: false, rownumbers: true, alternatingRow: true, enabledEdit: true, onAfterEdit: f_onAfterEdit,
		columns: [
			{ display: 'tmaipd_id', name: 'tmaipd_id', hide: true, width:1 , type: 'string'},
			{ display: 'tmad_id', name: 'tmad_id', hide: true, width:1 , type: 'string'},
			{ display: 'org_id', name: 'org_id', hide: true, width:1 , type: 'string'},
			{ display: '机构名称', name: 'caption', width:1, hide: true, type: 'string'},
			{ display: '药具种类', name: 'tmgd_name', width: 120, align: 'left' ,type: 'string'},
			{ display: '药品名称', name: 'tmad_name', width: 150, align: 'left' ,type: 'string'},			
			{ display: '规格', name: 'tmad_spec', width: 50, align: 'left' ,type: 'string'},
			{ display: '单位', name: 'tmad_unit', width: 50, align: 'left' ,type: 'string'},
			{ display: '每件内装', name: 'tmad_install', width: 80, align: 'left' ,type: 'string'},
			{ display: '单价(元)', name: 'tmad_price', width: 85, align: 'left' ,type: 'float' },
			{ display: '数量', name: 'tmaad_num', width: 70, align: 'left' ,type: 'int', editor: { type: 'int'}},
			{ display: '金额(元)', name: 'money', width: 75, align: 'left', isSort: false,type: 'float',render: function(record, index, value, oth){
					return formatCurrency(record.tmad_price * record.tmaad_num);
				}
			},
			{ display: '折合件数', name: 'shuliang', width: 75, align: 'left', type: 'int', editor: { type: 'int'}},
			{ display: '有效期', name: 'tmad_qgp', width: 1, hide:true, align: 'left', type: 'int'},
			{ display: '备注', name: 'tmad_memo', width: 80, align: 'left', type: 'string', editor: { type: 'string'}}
		],
		toolbar: {
			items: [
				{ text: '计算', click:jisuan, icon: 'add' }
			]
		}
	});
	//detail
	detail = $("#detail").ligerGrid({
		url: _url, width: '890', height:'325',async:false,delayLoad:true,
		usePager: false, rownumbers: true, alternatingRow: true, enabledEdit: true, onAfterEdit: f_onAfterEdit,
		columns: [
			{ display: 'tmaipd_id', name: 'tmaipd_id', hide: true, width:1 , type: 'string'},
			{ display: 'tmad_id', name: 'tmad_id', hide: true, width:1 , type: 'string'},
			{ display: 'org_id', name: 'org_id', hide: true, width:1 , type: 'string'},
			{ display: '机构名称', name: 'caption', width:1, hide: true, type: 'string'},
			{ display: '药具种类', name: 'tmgd_name', width: 120, align: 'left' ,type: 'string'},
			{ display: '药品名称', name: 'tmad_name', width: 150, align: 'left' ,type: 'string'},			
			{ display: '规格', name: 'tmad_spec', width: 50, align: 'left' ,type: 'string'},
			{ display: '单位', name: 'tmad_unit', width: 50, align: 'left' ,type: 'string'},
			{ display: '每件内装', name: 'tmad_install', width: 85, align: 'left' ,type: 'string'},
			{ display: '单价(元)', name: 'tmad_price', width: 75, align: 'left' ,type: 'float' },
			{ display: '数量', name: 'tmaad_num', width: 75, align: 'left' ,type: 'int'},
			{ display: '金额(元)', name: 'money', width: 85, align: 'left',isSort: false, type: 'float',render: function(record, index, value, oth){
					return formatCurrency(record.tmad_price * record.tmaad_num);
				}
			},
			{ display: '折合件数', name: 'shuliang', width: 75, align: 'left', type: 'int'},
			{ display: '有效期', name: 'tmad_qgp', width: 1, hide:true, align: 'left', type: 'int'},
			{ display: '备注', name: 'tmad_memo', width: 80, align: 'left', type: 'string'}
		],
		toolbar: {
			items: [
				{ text: '导出', click:function(){expData(r_id);}, icon: 'xls' },
				{ line: true },
				{ text: '计算', click:function(){jisuan1();}, icon: 'add' }
			]
		}
	});
	$("#tmaipm_year").find("option").remove();
	for(var i = 2005;i<2030;i++){
    	$("#tmaipm_year").append('<option value="'+i+'">'+i+'</option>');
    }
});
function f_onAfterEdit(e)
{
	manager.updateCell('money', parseInt(e.record.tmaad_num) * parseFloat(e.record.tmad_price), e.record);
}
/*
 * 表格刷新
 */
function reload(){
	_jsonObj.methodname = "queryPage";
	_jsonObj.logicdata = "[]";
	grid.loadData();
}
/*
*新增数据
*/
function addData(){
	win = false;
	document.getElementById('tma_income_plan-form').reset();
	$("#manager-div").show();
	$("#tmaipm_time").ligerDateEditor({ showTime: true, width:180});
	$("#tmaipm_time").val(shijian.substring(0,shijian.lastIndexOf(":")));
	var nian = shijian.substring(0,shijian.indexOf("-"));
	$("#tmaipm_year").val(nian);
	$("#create_by").val(userName);
	$("#org_id").val(org);
	$("#tmaipm_id").val("");
	$("#a1").html("0.00");
	var param = {
		"classname": "com.wxtf.platform.main.contraceptives.TmaIncomePlanMainAction",
		"methodname": "yao",
		"logicdata": [],
		"extenddata": []
	};
	param.logicdata = '[{"year":"'+nian+'"}]';
	manager.setOptions({url:_url,parms: param});	
}
/*
 * 初始化窗口
 */
function initWindowByDivM(target, title, width, height, func, func2 ,func3){
	return $.ligerDialog.open({
		width: width,
		height: height,
		title:title,
		modal:true,
		allowClose: true,
		target: $("#"+target),
		buttons: [{
			text: '暂存', onclick: func
		}, {
			text: '提交', onclick: func2
		},{
			text: '取消', onclick: func3
		}]
	});
}
/*
*删除数据
*/
function delData(){
	var str = "";
    var rows = grid.getCheckedRows();
    $(rows).each(function (){
    	str += this.tmaipm_id + ",";
    });
    str=str.substring(0,str.length-1);
	if(str == ""){
		warningTip('请选择');
		return;
	}
	$.ligerDialog.confirm('确定要删除选中的数据吗？', function (opt) { 			
		if(opt){
			_jsonObj.methodname = "delete";
			_jsonObj.logicdata = "[{\"ids\":\"" + str + "\"}]";
			callserver(_jsonObj, function(){ reload(); }, function(){});
		}
	});
}
/*
*明细
*/
function Detail(row){
	r_id = row;
	$("#d_org_id").val(row.caption);
	$("#d_tmaipm_year").val(row.tmaipm_year);
	$("#d_tmaipm_time").val(row.tmaipm_time);
	$("#d_create_by").val(row.create_by);
	$("#d_money").val(0);
	var d_tmaipm_is_valid = "否";
	if("1"==row.tmaipm_is_valid){
		d_tmaipm_is_valid = "是";
	}
	$("#d_tmaipm_is_valid").val(d_tmaipm_is_valid);
	initWindowByDiv2('detail-div', '计划入库查看', 915, 480, closeWin);
	var param = {
		"classname": "com.wxtf.platform.main.contraceptives.TmaIncomePlanMainAction",
		"methodname": "modYao",
		"logicdata": [],
		"extenddata": []
	};
	param.logicdata = '[{"tmaipm_id":"'+row.tmaipm_id+'"}]';
	detail.setOptions({url:_url,parms: param});
}
/*
*修改
*/
function edit(row){
	win = false;
	document.getElementById('tma_income_plan-form').reset();
	loadForm("tma_income_plan-form", row);
	$("#org_id").val(row.caption);
	$("#tmaipm_time").ligerDateEditor({ showTime: true, width:180});
	$("#tmaipm_time").val(row.tmaipm_time);
	$("#a1").html("0.00");
	$("#manager-div").show();
	var param = {
		"classname": "com.wxtf.platform.main.contraceptives.TmaIncomePlanMainAction",
		"methodname": "modYao",
		"logicdata": [],
		"extenddata": []
	};
	param.logicdata = '[{"tmaipm_id":"'+row.tmaipm_id+'"}]';
	manager.setOptions({url:_url,parms: param});
}
//合计
function jisuan(){
	var no = 0.00;            
	var jsonObj = {};
    jsonObj.Rows = [];
	$.each(manager.getData(), function (i, item) {
		jsonObj.Rows.push({
			tmaipd_id:item.tmaipd_id,
        	tmad_id:item.tmad_id,
			org_id:item.org_id,
			caption:item.caption,
			tmgd_name:item.tmgd_name,
			tmad_name:item.tmad_name,			
			tmad_spec:item.tmad_spec,
			tmad_unit:item.tmad_unit,
			tmad_install:item.tmad_install,
			tmad_price:item.tmad_price,
			tmaad_num:item.tmaad_num,
			money:parseFloat(item.tmad_price) * parseInt(item.tmaad_num),
			shuliang:item.shuliang,
			tmad_qgp:item.tmad_qgp,
			tmad_memo:item.tmad_memo
        });
        no = no + parseFloat(item.tmad_price) * parseInt(item.tmaad_num);
    });
    manager.set({ data: jsonObj });
    $("#a1").html(formatCurrency(no));
}
function jisuan1(){
	var no = 0.00;
	$.each(detail.getData(), function (i, item) {
        no = no + parseFloat(item.tmad_price) * parseInt(item.tmaad_num);
    });
    $("#d_money").val(formatCurrency(no));
}

/*
*新建
*/
function insert(s){

	var tmaipm_time = $("#tmaipm_time").val().trim();
	if(s == 0){
		$("#tmaipm_is_valid").val("0");
	}else{
		$("#tmaipm_is_valid").val("1");
	}
	if('' == tmaipm_time){warningTip("制表日期：不能为空"); return;}
		var param = {
			"classname": "com.wxtf.platform.main.contraceptives.TmaIncomePlanMainAction",
			"methodname": "",
			"logicdata": [],
			"extenddata": []
		};
		var tmaipm_id = $("#tmaipm_id").val();
		if(tmaipm_id==""){
			param.methodname = 'save';
		}else{
			param.methodname = 'update';
		}		
		var data = getForm("tma_income_plan-form");
		var d = data.substring(0,data.length-2);
		var mingxi = [];
		$.each(manager.getData(), function (i, item) {
			mingxi.push({
				tmaipd_id:item.tmaipd_id,
        		tmad_id:item.tmad_id,
				tmaid_num:item.tmaad_num,
				tmaipm_packages_num:item.shuliang
        	});
    	});
		d = d+",ids:"+JSON.stringify(mingxi)+"}]";
		param.logicdata = d;
		callserver(param, ajaxCallbackSuccFunc, ajaxCallbackErrorFunc);
}
/*
 * ajax成功回调
 */
function ajaxCallbackSuccFunc(data, textStatus){
	win = true; 
	$("#manager-div").hide();
	succMsgTip(data.resultrecord);
	reload();
}

/*
 * ajax失败回调
 */
function ajaxCallbackErrorFunc(data, textStatus){
	warningTip(data.resultrecord);
}
/*
*选择年
*/
function changeTmaipmYear(v){
	var param = {
		"classname": "com.wxtf.platform.main.contraceptives.TmaIncomePlanMainAction",
		"methodname": "yao",
		"logicdata": [],
		"extenddata": []
	};
	param.logicdata = '[{"year":"'+v+'"}]';
	manager.setOptions({url:_url,parms: param});
}
/*
*取消
*/
function quxiao(){
	win = true;
	$("#manager-div").hide();
}
/*
 * 导出
 */
function expData(record){
	var param = {
		"classname": "com.wxtf.platform.main.contraceptives.TmaIncomePlanMainAction",
		"methodname": "expData",
		"logicdata": [],
		"extenddata": []
	};
	param.logicdata = "[{\"tmaipm_id\":\""+record.tmaipm_id+"\"}]";
	
	callserver(param, function(data, textStatus){
		if(data.result){
			downloadFile(data.resultrecord);
		} else {
			warningTip(data.resultrecord);
		}
	}, function(data, textStatus){
	});
}
function formatCurrency(num)
        {
            if (!num) return "0.00";
            num = num.toString().replace(/\$|\,/g, '');
            if (isNaN(num))
                num = "0.00";
            sign = (num == (num = Math.abs(num)));
            num = Math.floor(num * 100 + 0.50000000001);
            cents = num % 100;
            num = Math.floor(num / 100).toString();
            if (cents < 10)
                cents = "0" + cents;
            for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
                num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
            return (((sign) ? '' : '-') + '' + num + '.' + cents);
        }
</script>
</head>
	<body>
		<div id="grid"></div>
		<!-- detail -->
		<div id="detail-div" style="display: none">
				<table class="form_table">
					<tr>
						<td class="form_label">编报单位：</td>
						<td>
							<input disabled="disabled" class="textbg1" id="d_org_id" name="d_org_id"/>
						</td>
						<td class="form_label">计划年度：</td>
						<td>
							<input disabled="disabled" class="textbg1" id="d_tmaipm_year" name="d_tmaipm_year"/>
						</td>
					</tr>
					<tr>						
						<td class="form_label">制表日期：</td>
						<td align="center">
							<input disabled="disabled" class="textbg1" id="d_tmaipm_time" name="d_tmaipm_time"/>
						</td>
						<td class="form_label">制表人：</td>
						<td>
							<input disabled="disabled" class="textbg1" id="d_create_by" name="d_create_by"/>
						</td>
					</tr>
					<tr>
						<td class="form_label">是否提交：</td>
						<td>
							<input disabled="disabled" class="textbg1" id="d_tmaipm_is_valid" name="d_tmaipm_is_valid"/>
						</td>
						<td class="form_label">总金额：</td>
						<td>
							<input disabled="disabled" class="textbg1" id="d_money" name="d_money"/>
						</td>
					</tr>
					<tr>
						<td colspan="4"><div id="detail"></div></td>
					</tr>			
				</table>
		</div>
		<!-- add -->
		<div id="manager-div" style="display:none; background-color: white; position: absolute; top: 20px; left: 50px;background: url('resource/images/pbg.jpg') repeat;">
		<div align="left" style="background: url('ligerUI/skins/Aqua/images/win/dialog-tc.gif') repeat-x; font-size: 15px; color: white;">年度计划</div>
		<div style="background-color: white; margin-left: 5px; margin-right: 5px; margin-bottom: 5px;">
		<div>
			<form id="tma_income_plan-form" name="tma_income_plan-form">
			<input type="hidden" id="tmaipm_id" name="tmaipm_id"/>
			<input type="hidden" id="tmaipm_code" name="tmaipm_code"/>
			<input type="hidden" id="tmaipm_type" name="tmaipm_type" value="0"/>
				<table class="form_table">
					<tr>
						<td class="form_label">编报单位：</td>
						<td>
							<input disabled="disabled" class="textbg1" id="org_id" name="org_id"/>
						</td>
						<td class="form_label">计划年度：</td>
						<td>
							<select id="tmaipm_year" name="tmaipm_year" onchange="changeTmaipmYear(this.value);" style=" width: 182px;">
							</select>
						</td>
					</tr>
					<tr>						
						<td class="form_label">制表日期：</td>
						<td align="center"><input onkeyup="this.value=formatdata(this.value);" id="tmaipm_time" name="tmaipm_time"/></td>
						<td class="form_label">制表人：</td>
						<td><input readonly="readonly" class="textbg1" id="create_by" name="create_by" /></td>
					</tr>
					<tr>
						<td class="form_label" style="display: none;">是否提交：</td>
						<td style="display: none;">
							<select id="tmaipm_is_valid" name="tmaipm_is_valid" style=" width: 182px;">
								<option value="1">是</option>
								<option value="0" selected="selected">否</option>
							</select>
						</td>
						<td></td>
						<td></td>
						<td class="form_label">总金额：</td>
						<td id="a1"></td>
					</tr>
					<tr>
						<td colspan="4"><div id="manager"></div></td>
					</tr>			
				</table>
			</form>
		</div>
		<div align="right" style="background-color:#F5F8F9; margin-top: 5px;"><input style=" width: 80px; margin: 3px;" type="button" value="暂存" onclick="insert(0);"/><input style=" width: 80px; margin: 3px;" type="button" value="提交" onclick="insert(1);"/><input style=" width: 80px; margin: 3px;" type="button" value="取消" onclick="quxiao();"/></div>
		</div>
		</div>
	</body>
</html>